<template>
  <div class="login-top">
    <div class="login-top-login">
      登录
    </div>
    <div class="login-top-title">
      欢迎登录系统，请输入用户名和密码进入平台
    </div>
  </div>
  <!--  -->
  <div class="login-from">
    <el-input :prefix-icon="User" class="login-from-input" v-model="loginfrom.username" placeholder="请输入用户名" />
    <el-input :prefix-icon="Lock
      " show-password class="login-from-input" v-model="loginfrom.password" placeholder="请输入密码" />
  </div>
  <!--  -->
  <div class="login-pass">
    <el-checkbox v-model="loginfrom.checked" class="login-pass-checkbox">
      记住密码
    </el-checkbox>
    <div class="login-pass-forgot" @click="forgotbtn">忘记密码?</div>
  </div>
  <!--  -->

  <div class="login-sub">
    <el-button type="primary" class="login-sub-btn" @click="loginbtn">登录</el-button>


    <el-button @click="registerbtn" class="login-sub-btn">注册</el-button>


  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { postlogin, getusers } from '@/api'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'
//  管理员  admin 123
//  用户    root  123
const loginfrom = ref({
  username: '',
  password: '',
  checked: false,
})

const userStore = useUserStore();
const router = useRouter();

// 页面加载时，从本地存储获取账号密码
onMounted(() => {
  const savedLoginInfo = localStorage.getItem('loginInfo');
  if (savedLoginInfo) {
    const parsedInfo = JSON.parse(savedLoginInfo);
    loginfrom.value.username = parsedInfo.username;
    loginfrom.value.password = parsedInfo.password;
    loginfrom.value.checked = parsedInfo.checked;
  }
});

const loginbtn = async () => {
  try {
    if (loginfrom.value.username === '' && loginfrom.value.password === '') {
      ElMessage.error('请输入用户名和密码')
      return
    }
    // 登录获取token
    const loginRes = await postlogin({ username: loginfrom.value.username, password: loginfrom.value.password })

    if (loginRes.code === 200) {
      // 设置token
      userStore.setToken(loginRes.data.token)

      // 获取用户信息
      const userRes = await getusers()
 
      if (userRes.code === 200) {
        // 设置用户信息
        userStore.setUserInfo(userRes.data)

        // 如果用户选择了记住密码，则保存到本地存储
        if (loginfrom.value.checked) {
          const loginInfo = {
            username: loginfrom.value.username,
            password: loginfrom.value.password,
            checked: loginfrom.value.checked
          };
          localStorage.setItem('loginInfo', JSON.stringify(loginInfo));
        } else {
          // 如果用户没有选择记住密码，则清除本地存储
          localStorage.removeItem('loginInfo');
        }

        console.log('登录成功')
        // 登录成功后跳转到主页面
        router.push('/');
      }
    }
  } catch (error) {
    console.error('登录失败:', error)
  }
}

const registerbtn = () => {
  loginfrom.value = {
    username: '',
    password: '',
    checked: false,
  }
  router.push('/register')
}

const forgotbtn = () => {
  loginfrom.value = {
    username: '',
    password: '',
    checked: false,
  }
  router.push('/forgotpass')
}

</script>

<style lang="less" scoped>
.login-top {



  .login-top-login {
    // width: 60px;
    height: 42px;
    font-family: PingFangSC, PingFang SC;
    font-weight: 600;
    font-size: 30px;
    color: #FFFFFF;
    line-height: 42px;
    text-align: left;
    font-style: normal;
  }

  .login-top-title {
    // width: 411px;
    height: 28px;
    font-family: PingFangSC, PingFang SC;
    margin-top: 20px;
    font-size: 18px;
    color: #8692A6;
    line-height: 28px;
    text-align: left;
    font-style: normal;
  }
}

.login-from {
  margin-top: 50px;

  .login-from-input {
    height: 64px;

    :deep(.el-input__wrapper) {
      border: 1px solid rgba(207, 224, 255, 0.46);
      background: rgba(16, 23, 66, 0.59);
      border-radius: 4px;
    }
  }

  .login-from-input:last-child {
    margin-top: 50px;
  }
}

.login-pass {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 30px;

  .login-pass-checkbox {

    height: 22px;
    font-size: 16px;
    color: #8692A6;
    line-height: 22px;

    font-style: normal;

    :deep(.el-checkbox__input.is-checked+.el-checkbox__label) {
      color: rgba(172, 195, 233, 1);
    }
  }

  .login-pass-forgot {
    height: 22px;
    font-size: 16px;
    color: #8692A6;
    line-height: 22px;
    cursor: pointer;
    font-style: normal;
  }

  .login-pass-forgot:active {
    color: rgba(172, 195, 233, 1);
  }
}

.login-sub {
  // display: flex;
  margin-top: 30px;

  .login-sub-btn {
    width: 100%;
    height: 64px;
    background: #5871EB;
    border-radius: 4px;
    border: none;
  }

  .login-sub-btn:hover {
    color: none;
  }

  :deep(.el-button+.el-button) {
    margin: 0;
    margin-top: 10px;
    background: none;

  }

  :deep(.el-button+.el-button:hover) {
    color: rgba(134, 146, 166, 1);

  }

  :deep(.el-button+.el-button:active) {
    color: rgba(172, 195, 233, 1);

  }
}
</style>